package com.campus.help.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.help.common.Result;
import com.campus.help.common.ResultCode;
import com.campus.help.service.SchoolService;
import com.campus.help.vo.SchoolVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学校控制器
 *
 * @author campus-help
 * @since 2025-09-18
 */
@Slf4j
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    /**
     * 获取所有学校列表（不分页）
     */
    @GetMapping("/list")
    public Result<List<SchoolVO>> getSchoolList() {
        try {
            List<SchoolVO> schoolList = schoolService.getSchoolList();
            return Result.success(schoolList);
        } catch (Exception e) {
            log.error("获取学校列表失败", e);
            return Result.error(ResultCode.SYSTEM_ERROR);
        }
    }

    /**
     * 根据ID获取学校信息
     */
    @GetMapping("/{id}")
    public Result<SchoolVO> getSchoolById(@PathVariable Long id) {
        try {
            SchoolVO school = schoolService.getSchoolById(id);
            if (school == null) {
                return Result.error(ResultCode.NOT_FOUND);
            }
            return Result.success(school);
        } catch (Exception e) {
            log.error("获取学校信息失败", e);
            return Result.error(ResultCode.SYSTEM_ERROR);
        }
    }
}

/**
 * 学校控制器（复数路径，用于小程序端）
 */
@Slf4j
@RestController
@RequestMapping("/schools")
class SchoolsController {

    @Autowired
    private SchoolService schoolService;

    /**
     * 分页获取学校列表（支持状态过滤）
     * 
     * @param page 页码，默认1
     * @param size 每页大小，默认10
     * @param status 状态：1-启用，0-禁用
     * @param keyword 关键词（可选）
     * @param province 省份（可选）
     * @param city 城市（可选）
     * @return 分页学校列表
     */
    @GetMapping("/list")
    public Result<Page<SchoolVO>> getSchoolPageList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city) {
        try {
            log.info("获取学校列表 - page: {}, size: {}, status: {}, keyword: {}, province: {}, city: {}", 
                    page, size, status, keyword, province, city);
            
            Page<SchoolVO> schoolPage = schoolService.getSchoolPageList(page, size, status, keyword, province, city);
            return Result.success(schoolPage);
        } catch (Exception e) {
            log.error("获取学校列表失败", e);
            return Result.error("获取学校列表失败: " + e.getMessage());
        }
    }
}
